package com.imucs.campus_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imucs.campus_admin.entity.Admin;
import com.imucs.campus_admin.service.AdminService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;
import util.JwtUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    @PostMapping("/login")
    public Result login(@RequestBody Map m){
        String username = (String) m.get("username");
        String password = (String) m.get("password");
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Admin admin = adminService.getOne(queryWrapper);


        if(admin != null){
            if(admin.getPassword().equals(password)){
                admin.setLoginDate(new Date());
                if(!adminService.saveOrUpdate(admin)){
                    return  new Result(false,StatusCode.ERROR,"登录时间记录失败");
                }

                Map<String, Object> info = new HashMap<>();
                info.put("username", username);
                info.put("password", password);

                //生成JWT字符串
                String token = JwtUtil.sign(admin.getId(), info);





                return  new Result(true,StatusCode.OK,"登录成功",admin);
            }else{
                return new Result(false,StatusCode.ERROR,"密码错误");
            }
        }else{
            return new Result(false,StatusCode.ERROR,"用户名不存在");
        }
    }

    @GetMapping("/list")
    public Result list(){
        return new Result(true,StatusCode.OK,"查询成功",adminService.list());
    }

    @PostMapping("/selectBy")
    public Result selectBy(@RequestBody Map m){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();

        if (null!=(Integer) m.get("id")){
            Integer adminId=(Integer) m.get("id");
            queryWrapper.eq("id",adminId);
        }

        if (null!=(String) m.get("username")){
            String username=(String) m.get("username");
            queryWrapper.eq("username",username);
        }

        List<Admin> adminlist = adminService.list(queryWrapper);
        if (0!=adminlist.size()){
            return new Result(true,StatusCode.OK,"查询成功",adminlist);
        }else{
            return new Result(false,StatusCode.ERROR,"查询失败");
        }

    }

    @PostMapping("/deleteByUserName")
    public Result deleteById(@RequestBody Map m){
        String username = (String) m.get("username");
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);

        if (adminService.remove(queryWrapper)){
            return new Result(true,StatusCode.OK,"删除成功");
        }else{
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Map m){

        String username =(String) m.get("username");
        String password =(String) m.get("password");

        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setId(new IdWorker().nextId()+"");

        if (adminService.save(admin)) {
            return new Result(true,StatusCode.OK,"添加成功",admin);
        }else {
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("update")
    public Result update(@RequestBody Map m){

        String username = (String) m.get("username");
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Admin admin = adminService.getOne(queryWrapper);

        if (null!=admin){
            if (null!=(Integer)m.get("id")){
                admin.setId((String)m.get("id"));
            }
            if (null!=(String)m.get("password")){

                admin.setPassword((String)m.get("password"));
            }
            if (null!=(String)m.get("username")){

                admin.setUsername((String)m.get("username"));
            }

            if (adminService.saveOrUpdate(admin)) {
                return new Result(true,StatusCode.OK,"修改成功",admin);
            }else{
                return new Result(false,StatusCode.ERROR,"数据没有成功保存");
            }

        }else{
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
    }

}
